package com.example.weibo3;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.weibo3.adapter.CartAdapter;
import com.example.weibo3.bean.MessageItem;
import com.example.weibo3.bean.ResultBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MsgFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MsgFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MsgFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MsgFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MsgFragment newInstance(String param1, String param2) {
        MsgFragment fragment = new MsgFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_msg, container, false);
        // 请求逻辑代码
        // 4. 利用okhttp库发起http get请求
        //===============网络请求================
        OkHttpClient client = new OkHttpClient();     // 初始化client对象
        // 配置参数
        Request request = new Request.Builder()
                .url("http://129.28.159.96:7001/v1/test/getMessageList")  // 请求接口地址
                .build();

        // 发起请求，在请求的时候，okhttp新建一个线程
        client.newCall(request).enqueue(new Callback() {
            // 失败
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                //错误处理
                Log.e("tag", e.toString());
            }

            // 成功
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                // 获取返回数据
                String data = response.body().string();   // 取出数据
                Log.i("tag", data);
                // 通过gson把字符串转成对象
                Gson gson = new Gson();
                ArrayList<MessageItem> dataList =
                        gson.fromJson(data, new TypeToken<  ArrayList<MessageItem>  >(){}.getType());

                CartAdapter cartAdapter = new CartAdapter(getActivity(), R.layout.list_view_item, dataList);
                ListView listView = view.findViewById(R.id.list_view);

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 切换到主线程执行下面的代码
                        listView.setAdapter(cartAdapter);   // 更新界面
                    }
                });

            }
        });
        //=========================================

        return view;
    }
}